using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace PaintIsland
{
    public class Level
    {
        private Dictionary<Vector3Int, BlockID[,,]> chunkBuffer;

        private static Level instance;
        private Level()
        {
            chunkBuffer = new Dictionary<Vector3Int, BlockID[,,]>();
        }

        public static Level GetInstance()
        {
            if (instance == null) instance = new Level();
            return instance;
        }

        public void SaveChunk(Chunk chunk)
        {
            if (chunkBuffer.ContainsKey(chunk.ChunkLocation))
            {
                chunkBuffer[chunk.ChunkLocation] = (BlockID[,,])chunk.Data.Clone();
            }
            else
            {
                chunkBuffer.Add(chunk.ChunkLocation, (BlockID[,,])chunk.Data.Clone());
            }
        }

        public BlockID[,,] GetChunkData(Vector3 p)
        {
            return GetChunkData(new Vector3Int(Mathf.FloorToInt(p.x / Chunk.CHUNK_SIZE), Mathf.FloorToInt(p.y / Chunk.CHUNK_SIZE), Mathf.FloorToInt(p.z / Chunk.CHUNK_SIZE)));
        }

        public BlockID[,,] GetChunkData(Vector3Int position)
        {
            if (chunkBuffer.TryGetValue(position, out BlockID[,,] data))
            {
                return data;
            }
            return null;
        }
    }
}